package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.UserInfo;

import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/**
 * @author fan
 * @Description:
 * @since 2023/01/10 14:47
 */
@RestController
@RequestMapping("shopping")
public class OrderController {
    @Autowired
    OrderCoreService orderCoreService;
    @Autowired
    OrderQueryService orderQueryService;

    //fan
    @PostMapping("order")
    public ResponseData creatOrder(@RequestBody CreateOrderRequest orderRequest, HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");
        UserInfo user = JSON.parseObject(userInfo, UserInfo.class);
        orderRequest.setUserId(((long) user.getUid()));
        orderRequest.setUniqueKey(UUID.randomUUID().toString());
        String orderId = orderCoreService.createOrder(orderRequest);
        //TODO  待测试
        return new ResponseUtil<>().setData(orderId);
    }

    //谢畅
    @GetMapping("order")
    public ResponseData ListOrder(OrderListRequest orderListRequest, HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");
        UserInfo user = JSON.parseObject(userInfo, UserInfo.class);
        orderListRequest.setUserId(user.getUid());
        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);
        return new ResponseUtil<>().setData(orderListResponse);

    }

    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map) {
        String orderId = (String) map.get("orderId");
        orderCoreService.cancelOrder(orderId);
        return new ResponseUtil<>().setData(OrderRetCode.SUCCESS.getMessage());
    }

    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable Long id) {
        orderCoreService.deleteOrder(String.valueOf(id));
        return new ResponseUtil<>().setData(OrderRetCode.SUCCESS.getMessage());
    }

//    陈健冬
    @GetMapping("order/{id}")
    public ResponseData orderDetail(@PathVariable String id){
        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(id);
        return   new ResponseUtil<>().setData(orderDetailResponse);
    }
}
